<?php
use Phalcon\Tag as Tag;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;

class ServerUsersController extends ControllerBase
{
    public function initialize()
    {
        Tag::setTitle(' 平台用户');
        parent::initialize();
    }

    public function indexAction()
    {
        $this->session->conditions = null;
        $this->view->form = new ServerUsersForm();
    }

    /**
     * 创建平台用户表单
     *
     */
    public function newAction()
    {
        $this->view->form = new ServerUsersForm(null, array('edit' => true));
    }

    /**
     * new之后保存平台用户表单数据
     *
     * @return mixed
     */
    public function createAction()
    {
        if (!$this->request->isPost()) {
            return $this->forward('serverusers/index');
        }

        $form = new ServerUsersForm();
        $serverUser = new ServerUsers();
        $data = $this->request->getPost();
        if (!$form->isValid($data, $serverUser)) {
            foreach ($form->getMessages() as $message) {
                $this->flash->error($message);
            }
            return $this->forward('serverusers/new');
        }
        $data['addTime'] = $data['updateTime'] = date("Y-m-d H:i:s");
        if ($serverUser->save($data, array('user', 'name', 'password', 'phone', 'mobile', 'email', 'addTime', 'updateTime')) == false) {
            foreach ($serverUser->getMessages() as $message) {
                $this->flash->error($message);
                echo $message;
            }
            return $this->forward('serverusers/new');
        }

        $form->clear();
        $this->flash->success("平台用户保存成功");
        return $this->forward("serverusers/search");
    }

    /**
     * 查询平台用户
     *
     * @return mixed
     */
    public function searchAction()
    {
        $numberPage = 1;
        $serverUsers = ServerUsers::find();
        if (count($serverUsers) == 0) {
            $this->flash->notice("没有数据");
            return $this->forward("index/index");
        }


        $system = new Paginator(array(
            "data"  => $serverUsers,
            "limit" => 10,
            "page"  => $numberPage
        ));

        $this->view->page = $serverUsers;
        //$this->view->page = $paginator->getPaginate();
    }

    /**
     *修改平台用户
     *
     * @param $id
     * @return mixed
     */
    public function editAction($id)
    {
        if (!$this->request->isPost()) {
            $serverUserId = ServerUsers::findFirstById($id);
            if (!$serverUserId) {
                $this->flash->error("平台用户修改失败");
                return $this->forward("businessusers/search");
            }
            $this->view->form = new ServerUsersForm($serverUserId, array('edit' => true));
        }

    }

    public function saveAction()
    {
        if(!$this->request->isPost())
        {
            $this->flash->error("方法不是POST");
            return $this->forward('businessusers/index');
        }

        $id = $this->request->getPost("id", "int");
        $serverUserId = ServerUsers::findFirstById($id);
        if(!$serverUserId)
        {
            $this->flash->error("serverusers 没有数据");
            return $this->forward("serverusers/search");
        }

        $form = new ServerUsersForm($serverUserId, array('edit' => true));
        $this->view->form = $form;
        $data = $this->request->getPost();

        if (!$form->isValid($data, $serverUserId))
        {
            foreach ($form->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward("serverusers/edit/" . $id);
        }

        if ($serverUserId->save() == false)
        {
            foreach ($serverUserId->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('serverusers/edit/' . $id);
        }

        $form->clear();
        $this->flash->success("平台用户保存成功");
        return $this->forward("serverusers/search");
    }

    public function deleteAction($id)
    {

        $userId = ServerUsers::findFirstById($id);
        if (!$userId) {
            $this->flash->error("Product was not found");
            return $this->forward("serverusers/index");
        }

        if (!$userId->delete()) {

            foreach ($userId->getMessages() as $message) {
                $this->flash->error($message);
            }
            return $this->forward("serverusers/search");

        }

        $this->flash->success("平台用户删除成功");
        return parent::forward("serverusers/search");
    }
}